|
1
|
|
|
import clc from 'cli-color' |
|
2
|
|
|
import path from 'path' |
|
3
|
|
|
import fse from 'fs-extra' |
|
4
|
|
|
|
|
5
|
|
|
// ./node_modules/.bin/babel-node src/cli/process/publish-all.js ABE_WEBSITE=/path/to/website |
|
6
|
|
|
// ./node_modules/.bin/babel-node src/cli/process/publish-all.js FILEPATH=/path/to/website/path/to/file.html ABE_WEBSITE=/path/to/website |
|
7
|
|
|
var pConfig = {} |
|
8
|
|
|
Array.prototype.forEach.call(process.argv, (item) => { |
|
9
|
|
|
if (item.indexOf('=') > -1) { |
|
10
|
|
|
var ar = item.split('=') |
|
11
|
|
|
pConfig[ar[0]] = ar[1] |
|
12
|
|
|
} |
|
13
|
|
|
}) |
|
14
|
|
|
|
|
15
|
|
|
if(typeof pConfig.ABE_PATH === 'undefined' || pConfig.ABE_PATH === null) { |
|
16
|
|
|
pConfig.ABE_PATH = '' |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
console.log(clc.green('start publish all') + ' path ' + pConfig.ABE_PATH) |
|
|
|
|
|
|
20
|
|
|
|
|
21
|
|
|
function msToTime(duration) { |
|
22
|
|
|
var milliseconds = parseInt((duration%1000)/100) |
|
23
|
|
|
, seconds = parseInt((duration/1000)%60) |
|
24
|
|
|
, minutes = parseInt((duration/(1000*60))%60) |
|
25
|
|
|
, hours = parseInt((duration/(1000*60*60))%24) |
|
26
|
|
|
|
|
27
|
|
|
hours = (hours < 10) ? '0' + hours : hours |
|
28
|
|
|
minutes = (minutes < 10) ? '0' + minutes : minutes |
|
29
|
|
|
seconds = (seconds < 10) ? '0' + seconds : seconds |
|
30
|
|
|
|
|
31
|
|
|
return hours + ':' + minutes + ':' + seconds + '.' + milliseconds |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
function publishNext(published, tt, cb, i = 0) { |
|
35
|
|
|
var currentDateStart = new Date() |
|
36
|
|
|
var pub = published.shift() |
|
37
|
|
|
if(typeof pub !== 'undefined' && pub !== null) { |
|
38
|
|
|
|
|
39
|
|
|
var json = FileParser.getJson(pub.path) |
|
40
|
|
|
if(typeof json.abe_meta !== 'undefined' && json.abe_meta !== null) { |
|
41
|
|
|
i++ |
|
42
|
|
|
|
|
43
|
|
|
var p = new Promise((resolve, reject) => { |
|
|
|
|
|
|
44
|
|
|
try { |
|
45
|
|
|
|
|
46
|
|
|
cmsOperations.save.save( |
|
47
|
|
|
pub.path, |
|
48
|
|
|
json.abe_meta.template, |
|
49
|
|
|
null, |
|
50
|
|
|
'', |
|
51
|
|
|
'publish', |
|
52
|
|
|
null, |
|
53
|
|
|
'publish', |
|
54
|
|
|
true) |
|
55
|
|
|
.then(() => { |
|
56
|
|
|
var d = new Date(new Date().getTime() - currentDateStart.getTime()) |
|
57
|
|
|
var total = new Date(new Date().getTime() - dateStart.getTime()) |
|
58
|
|
|
// logsPub += i + ' [' + d + 'sec] > start publishing ' + pub.path .replace(config.root, '') + ' < ' + pub.path |
|
59
|
|
|
console.log(clc.green(i) + '/' + tt + ' - (' + clc.green(msToTime(d)) + '/' + msToTime(total) + ')') |
|
|
|
|
|
|
60
|
|
|
console.log(clc.bgWhite(clc.black(pub.path.replace(config.root, '').replace(config.data.url, ''))) |
|
61
|
|
|
+ ' (' + clc.cyan(json.abe_meta.template) + ')') |
|
62
|
|
|
resolve() |
|
63
|
|
|
}).catch(function(e) { |
|
64
|
|
|
var d = new Date(new Date().getTime() - currentDateStart.getTime()) |
|
65
|
|
|
var total = new Date(new Date().getTime() - dateStart.getTime()) |
|
66
|
|
|
console.log(clc.red(i) + '/' + tt + ' - (' + clc.red(msToTime(d)) + '/' + msToTime(total) + ')') |
|
|
|
|
|
|
67
|
|
|
publishErrors.push({ |
|
68
|
|
|
msg: e + '' |
|
69
|
|
|
, json:json |
|
70
|
|
|
}) |
|
71
|
|
|
console.log(clc.red(e)) |
|
72
|
|
|
console.log('publish-all', 'ERROR on ' + pub.path.replace(config.root, '').replace(config.data.url, '')) |
|
73
|
|
|
resolve() |
|
74
|
|
|
}) |
|
75
|
|
|
} catch(e) { |
|
76
|
|
|
console.log(clc.red('cannot save') + ' ' + pub.path) |
|
77
|
|
|
resolve() |
|
78
|
|
|
} |
|
79
|
|
|
}) |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
p.then(function () { |
|
|
|
|
|
|
83
|
|
|
publishNext(published, tt, cb, i++) |
|
84
|
|
|
}) |
|
85
|
|
|
.catch(function () { |
|
86
|
|
|
publishNext(published, tt, cb, i++) |
|
87
|
|
|
console.log('error', clc.red(e)) |
|
|
|
|
|
|
88
|
|
|
}) |
|
89
|
|
|
}else { |
|
90
|
|
|
cb(i) |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
var publishErrors = [] |
|
95
|
|
|
var dateStart = new Date() |
|
96
|
|
|
// var logsPub = "" |
|
97
|
|
|
if(typeof pConfig.ABE_WEBSITE !== 'undefined' && pConfig.ABE_WEBSITE !== null) { |
|
98
|
|
|
var config = require('../../cli').config |
|
99
|
|
|
if(pConfig.ABE_WEBSITE) config.set({root: pConfig.ABE_WEBSITE.replace(/\/$/, '') + '/'}) |
|
|
|
|
|
|
100
|
|
|
try { |
|
101
|
|
|
|
|
102
|
|
|
// require controller to instanciate hooks |
|
103
|
|
|
var controllers = require('../../server/controllers') |
|
104
|
|
|
var FileParser = require('../../cli').FileParser |
|
105
|
|
|
var fileUtils = require('../../cli').fileUtils |
|
106
|
|
|
var folderUtils = require('../../cli').folderUtils |
|
107
|
|
|
var cmsOperations = require('../../cli').cmsOperations |
|
108
|
|
|
var Manager = require('../../cli').Manager |
|
109
|
|
|
|
|
110
|
|
|
Manager.instance.init() |
|
111
|
|
|
.then(() => { |
|
112
|
|
|
var i = 0 |
|
|
|
|
|
|
113
|
|
|
|
|
114
|
|
|
var files = Manager.instance.getList() |
|
115
|
|
|
|
|
116
|
|
|
// var result = [] |
|
117
|
|
|
var published = [] |
|
118
|
|
|
var folderToParse = path.join(config.root, config.data.url, pConfig.ABE_PATH) |
|
119
|
|
|
Array.prototype.forEach.call(files, (file) => { |
|
120
|
|
|
if (typeof file.publish !== 'undefined' && file.publish !== null && file.path.indexOf(folderToParse) > -1) { |
|
121
|
|
|
published.push(file) |
|
122
|
|
|
} |
|
123
|
|
|
}) |
|
124
|
|
|
|
|
125
|
|
|
console.log('Found ' + clc.green(published.length) + ' to republish') |
|
|
|
|
|
|
126
|
|
|
|
|
127
|
|
|
dateStart = new Date() |
|
128
|
|
|
publishNext(published, published.length, function (i) { |
|
129
|
|
|
console.log('total ' + clc.green(i) + ' files') |
|
|
|
|
|
|
130
|
|
|
if (publishErrors.length > 0) { |
|
131
|
|
|
var errorPath = path.join(config.root, 'abe-publish-all.' + dateStart.getTime() + '.log') |
|
132
|
|
|
console.log('Errors ' + clc.red(publishErrors.length) + ' see ' + errorPath + ' for more info') |
|
133
|
|
|
fse.writeJsonSync(errorPath, publishErrors, { |
|
134
|
|
|
space: 2, |
|
135
|
|
|
encoding: 'utf-8' |
|
136
|
|
|
}) |
|
137
|
|
|
} |
|
138
|
|
|
dateStart = (Math.round((new Date().getTime() - dateStart.getTime()) / 1000 / 60 * 100)) / 100 |
|
139
|
|
|
console.log('publish process finished in ' + clc.green(dateStart) + 'm') |
|
140
|
|
|
process.exit(0) |
|
|
|
|
|
|
141
|
|
|
}) |
|
142
|
|
|
}) |
|
143
|
|
|
.catch((e) => { |
|
144
|
|
|
console.log('publish-all', e) |
|
|
|
|
|
|
145
|
|
|
}) |
|
146
|
|
|
|
|
147
|
|
|
} catch(e) { |
|
148
|
|
|
console.log('publish-all', e) |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
}else { |
|
152
|
|
|
console.log('ABE_WEBSITE is not defined use node process.js ABE_WEBSITE=/pat/to/website') |
|
153
|
|
|
process.exit(0) |
|
|
|
|
|
|
154
|
|
|
} |